//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jeecg.modules.business.excelUtils;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.jeecg.modules.business.excelUtils.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecg.modules.business.excelUtils.ExcelExportServer;
import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
import org.springframework.stereotype.Controller;

@Controller("jeecgEntityExcelView")
public class JeecgEntityExcelView extends MiniAbstractExcelView {
    public JeecgEntityExcelView() {
    }

    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String codedFileName = "临时文件";
        Workbook workbook = new SXSSFWorkbook();
        String[] exportFields = null;
        Object exportFieldStr = model.get("exportFields");
        if (exportFieldStr != null && exportFieldStr != "") {
            exportFields = exportFieldStr.toString().split(",");
        }

        if (model.containsKey("mapList")) {
            List<List<Map<String, Object>>> list = (List)model.get("mapList");
            if (list.size() == 0 || list.get(0).size() == 0) {
                throw new RuntimeException("MAP_LIST IS NULL");
            }
           // workbook = ExcelExportUtil.exportSheetListExcel(list, exportFields);
            for(int i = 0; i < list.size(); ++i) {
                (new ExcelExportServer()).createSheetList(workbook, list.get(i) , exportFields);
            }
            /*for(int j = 0 ; j < sheetlist.size() ; ++j ){
                List<Map<String , Object>> list = sheetlist.get(j) ;
                if (list.size() == 0) {
                    throw new RuntimeException("MAP_LIST IS NULL");
                }
                if(j == 0) {
                    workbook = ExcelExportUtil.exportExcel((ExportParams)((Map)list.get(0)).get("params"), (Class)((Map)list.get(0)).get("entity"), (Collection)((Map)list.get(0)).get("data"), exportFields);
                    for(int i = 1; i < list.size(); ++i) {
                        (new ExcelExportServer()).createSheet(workbook, (ExportParams)((Map)list.get(i)).get("params"), (Class)((Map)list.get(i)).get("entity"), (Collection)((Map)list.get(i)).get("data"), exportFields);
                    }
                }else {
                    for(int i = 0; i < list.size(); ++i) {
                        (new ExcelExportServer()).createSheet(workbook, (ExportParams)((Map)list.get(i)).get("params"), (Class)((Map)list.get(i)).get("entity"), (Collection)((Map)list.get(i)).get("data"), exportFields);
                    }
                }


            }*/

        } else {
            workbook = ExcelExportUtil.exportExcel((ExportParams)model.get("params"), (Class)model.get("entity"), (Collection)model.get("data"), exportFields);
        }

        if (model.containsKey("fileName")) {
            codedFileName = (String)model.get("fileName");
        }

        if (workbook instanceof HSSFWorkbook) {
            codedFileName = codedFileName + ".xls";
        } else {
            codedFileName = codedFileName + ".xlsx";
        }

        if (this.isIE(request)) {
            codedFileName = URLEncoder.encode(codedFileName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
        }

        response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }
}
